#!/usr/bin/env python
# -*- coding: utf-8 -*-
####
# Virgin Mobile Broadband2Go Connection Manager 0.11
####
# Requires PyGtk 2.0 or greater, xterm, usb_modeswitch, and gnome-ppp
# Please setup your path to your usb_modeswitch compiled binary
# as an example, I used mine, with is under my ~/src folder.
# This frontend is GPL opensource.
#
# coded by Adam "RabidWeezle" Smith: rabidweezle@gmail.com
###
import subprocess
import sys

import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade

######
## app paths, change these if you need to
######
##usb modeswitch using xterm so you can see output if it errors. Changed in 0.11 to reflect a more sane installer.
usbms = 'xterm -e "echo If this xterm window gets stuck pull out the usb dongle from the port and try again && echo this should not take more than 2 seconds tops && sudo /usr/share/vmdialer/usb_modeswitch"' 
#This shouldn't have to be changed... works in kubuntu and ubuntu jaunty
driver = "gksu modprobe usbserial vendor=0x1410 product=0x6002"
#This is the only method I know of that works
dialer = "gksu gnome-ppp"

def run_it(x):
  subprocess.Popen(x, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell="/bin/bash", cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

class VirginMobileGTK:
	"""This is an application to load the Broadband2GO
	device driver and start the dialer
	"""

	def __init__(self):
		
		#Set the Glade file
		self.gladefile = "virginmobile.glade"  
	        self.wTree = gtk.glade.XML(self.gladefile) 

		#Create our dictionay and connect it for button1 (usb_modeswitch)
		dic1 = { "on_button1_clicked" : self.button1_clicked,
			  "on_window1_destroy" : gtk.main_quit }
		self.wTree.signal_autoconnect(dic1)

		#Create our dictionay and connect it for button2 (driver and gnome-ppp init)
		dic2 = { "on_button2_clicked" : self.button2_clicked,
			  "on_window1_destroy" : gtk.main_quit }
		self.wTree.signal_autoconnect(dic2)

## These next 2 lines say what to do when the "switch to modem" button is clicked, change this to reflect your usb_modeswitch path
	def button1_clicked(self, widget):
		print "If it hangs, pull out the usb dongle, and try again" #if they run in terminal they will see this...
		run_it(usbms)
	
	def button2_clicked(self, widget):
		foo = driver+' && '+dialer
		run_it(foo)
	
		#Get the Main Window, and connect the "destroy" event
		self.window = self.wTree.get_widget("window1")
		if (self.window):
			self.window.connect("destroy", gtk.main_quit)


if __name__ == "__main__":
	VMGTK = VirginMobileGTK()
	gtk.main()